//
//  NavigatorPageViewController.m
//  ActiveDeck
//
//  Created by Laura McBride on 5/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NavigatorPageViewController.h"
#import "AD_ViewPort.h"
#import "AD_Page.h"
#import "AD_Button.h"
#import "AD_ViewPort_Button_Link.h"
#import "AD_Layer.h"
#import "AD_Image.h"
#import "AD_WebView.h"


@implementation NavigatorPageViewController

@synthesize pageImage, VPBTlinkSets;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    VPBTlinkSets = [[NSMutableArray alloc] init];
    LABTlinkSets = [[NSMutableArray alloc] init];
    
    
    [self AddViewPort];
    [self AddLayer];
    [self AddWebViews];

    
    [super viewDidLoad];
}




- (void)AddButton:(int)Index_VP
          scrollV:(UIScrollView *)scrollV
{
	
    int i=0;

	for(AD_Button *multiButton in [[pageOB.mut_Arr_ViewPorts objectAtIndex:Index_VP] AD_ButtonSets])
	{
        AD_ViewPort_Button_Link *VBLink = [AD_ViewPort_Button_Link alloc];
        VBLink.Index_VP = Index_VP;
        VBLink.Index_BT = i++;
        //NSLog(@"Button Index %d, ViewPort Index %d", VBLink.Index_BT, VBLink.Index_VP);

        [VPBTlinkSets addObject:VBLink];
        [VBLink release];
        
		UIImage *offImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOff]];
		UIImage *onImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOn]];
		CGRect buttonFrame = CGRectMake(multiButton.left, multiButton.top, multiButton.width, multiButton.height);
		NSArray *condition = [multiButton.Condition componentsSeparatedByString:@"=="];
        
		UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[newButton setFrame:buttonFrame];
		[newButton setTag:[VPBTlinkSets count]-1];
		[newButton setBackgroundImage:offImage forState:UIControlStateNormal];
		[newButton setBackgroundImage:onImage forState:UIControlStateHighlighted];
		[newButton setBackgroundImage:onImage forState:UIControlStateSelected];
		[newButton addTarget:self action:@selector(viewPortbuttonPress:) forControlEvents:UIControlEventTouchUpInside];
		
        [newButton setHidden:[self setCondition:condition]];
		[scrollV addSubview:newButton]; //Tested memory removed
	}
}
             

-(void)viewPortbuttonPress:(id)sender
{
	UIButton *pressButton = (UIButton *)sender;
    NSLog(@"ButtonLog%d", pressButton.tag);
    AD_ViewPort_Button_Link * VBLink = (AD_ViewPort_Button_Link*)[VPBTlinkSets objectAtIndex:pressButton.tag];
    NSLog(@"Press Button Index %d, ViewPort Index %d", VBLink.Index_BT, VBLink.Index_VP);

	AD_Button *multiButton = (AD_Button*)[[(AD_ViewPort*)[pageOB.mut_Arr_ViewPorts objectAtIndex:VBLink.Index_VP] AD_ButtonSets] objectAtIndex:VBLink.Index_BT];
	
    [self buttonAction:multiButton];
    
    if(hOffset != 0 || vOffset != 0){
        for (int i=0; i < [self.view subviews].count; i++) {
            if([[[self.view subviews] objectAtIndex:i] isKindOfClass:[UIScrollView class]]){
                
                UIScrollView *scrollView = [[self.view subviews] objectAtIndex:i];
                
                CGPoint pt = CGPointMake(hOffset, vOffset);
                [scrollView setContentOffset:pt animated:YES];  
            }       
        }
    }
}



- (void)AddViewPort{
    int i =0;
    for (AD_ViewPort* vp in pageOB.mut_Arr_ViewPorts)
    {
        UIScrollView *scrollV = [[UIScrollView alloc]init];
        scrollV.pagingEnabled = YES;
        scrollV.contentSize = CGSizeMake(vp.Width, vp.Height);
        int scrollVWidth = 0;
        int scrollVHeight = 0;
        
        if([pageOB.mut_Arr_Orientations containsObject:[NSNumber numberWithInt:4]] || 
           [pageOB.mut_Arr_Orientations containsObject:[NSNumber numberWithInt:3]])
        {
            scrollVWidth = 1024;
            scrollVHeight = 768;   
        } else {
            scrollVWidth = 768;
            scrollVHeight = 1024;
        }
                
        int scroll_X_offSet = 0;
        int scroll_Y_offSet = 0;
        
        if(vp.Width < scrollVWidth)
            scrollVWidth = vp.Width;
        if(vp.Height < scrollVHeight)
            scrollVHeight = vp.Height;
        
        if((vp.Width - vp.HorizontalOffSet) < scrollVWidth)
            scroll_X_offSet = vp.Width - scrollVWidth;
    
        if((vp.Height - vp.VerticalOffset) < scrollVHeight)
            scroll_Y_offSet = vp.Height - scrollVHeight;
            
        if(vOffset != 0){
            scroll_Y_offSet = vOffset;
        }
        
        if(hOffset != 0){
            scroll_X_offSet = hOffset;
        }
        
        CGPoint contentOffset = CGPointMake(scroll_X_offSet, scroll_Y_offSet);
        
        scrollV.frame = CGRectMake(vp.left, vp.top, scrollVWidth, scrollVHeight);
        scrollV.showsHorizontalScrollIndicator = NO;
        scrollV.showsVerticalScrollIndicator = NO;
        scrollV.scrollsToTop = NO;
        scrollV.scrollEnabled = YES;
        scrollV.delegate = self;
        scrollV.pagingEnabled = NO;
        scrollV.bounces = NO;
        scrollV.contentOffset = contentOffset;
        //Temporarily disabled pinch and zooming so that navigator slides behave normally.
        
        if(vp.ZoomEnabled)
        {
            scrollV.minimumZoomScale = 0.2;
            scrollV.maximumZoomScale = 4.0;
            [scrollV setZoomScale:1.0];
        }
        
        NSLog(@"NavigatorImage:%@", vp.ImageBG);
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:vp.ImageBG]]];
        imageV.frame = CGRectMake(vp.left, vp.top, vp.Width, vp.Height);
        [scrollV addSubview:imageV];
        [imageV release];
        
        for (AD_Image *img in vp.AD_ImageSets){
            UIImageView *vpImgView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:img.imagePath]]];
            vpImgView.frame = CGRectMake(img.left, img.top, img.width, img.height);
            NSArray *imgCondition = [img.Condition componentsSeparatedByString:@"=="];
            
            [vpImgView setHidden:[self setCondition:imgCondition]];
            [scrollV addSubview:vpImgView];
            [vpImgView release];
        }
        
        
        
        [self AddButton:i++ scrollV:scrollV];
        [self.view addSubview:scrollV];
        [scrollV release];
        
        oldWidth = vp.Width;
    }
}

-(void)AddWebViews{
    for (AD_ViewPort* vp in pageOB.mut_Arr_ViewPorts)
    {
        for(AD_WebView *webViewOB in vp.AD_WebViewSets){
            CGRect webFrame = CGRectMake(webViewOB.left, webViewOB.top, webViewOB.width, webViewOB.height);
            
            WebViewController *webView = [[WebViewController alloc] initWithFrame:webFrame];
            
            webView.url = [NSURL URLWithString:webViewOB.urlAddress];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:webView.url];
            [webView loadRequest:requestObj];
            
            [self.view addSubview:webView];
            [webView release];
        }
    }
}




- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [pageImage release];
    [LABTlinkSets release];
    [VPBTlinkSets release];
    [super dealloc];
}


@end
